const mongoose = require('mongoose');
const { ObjectId } = mongoose.Schema;

/**
 * Export model definition object.
 */
const userDefinedFieldSchema = mongoose.Schema({
  key: {
    type: ObjectId,
    required: true,
  },
  name: {
    type: String,
    required: true,
  },
  value: {
    type: String,
    default: '',
  },
}, { _id: false });

module.exports = {
  key: { type: String, unique: true },
  creatorId: ObjectId,
  projectId: ObjectId,
  testCaseCreate: { // 是否创建过测试用例
    type: Boolean, default: false,
  },
  versionId: {
    type: ObjectId,
    index: true,
  },
  fileId: {
    type: ObjectId,
    index: true,
  },
  functionName: String,
  functionFullName: String,
  functionNameArgs: String,
  parents: { type: Array, index: true },
  depth: { type: Number, default: 1 },
  specialOriginalId: Array,
  parentId: ObjectId,
  parentTemplateId: ObjectId, // 父模板元素id，只有模板下的模板元素此字段才有值
  rawTemplateId: ObjectId, // 特化根原型模板id
  mangledId: String,
  kind: Number,
  description: { type: String, default: '' },
  status: { type: Number, default: 0 },
  language: String,
  location: {
    line: { type: Number },
    column: { type: Number },
  },
  endLocation: {
    line: { type: Number },
    column: { type: Number },
  },
  hash: String,
  changed: {
    type: String,
    enum: ['added', 'modified'],
  },
  failedCount: { type: Number, default: 0 },
  succeedCount: { type: Number, default: 0 },
  statementCovered: { type: Number, default: 0 },
  statementTotal: { type: Number, default: 0 },
  branchCovered: { type: Number, default: 0 },
  branchTotal: { type: Number, default: 0 },
  conditionCovered: { type: Number, default: 0 },
  conditionTotal: { type: Number, default: 0 },
  decisionCovered: { type: Number, default: 0 },
  decisionTotal: { type: Number, default: 0 },
  mcdcCovered: { type: Number, default: 0 },
  mcdcTotal: { type: Number, default: 0 },
  invokedFuncTotal: { type: Number, default: 0 },
  invokedFuncCovered: { type: Number, default: 0 },
  invokeTotal: { type: Number, default: 0 },
  invokeCovered: { type: Number, default: 0 },
  taskId: String,
  unread: { type: Boolean, default: false },
  unreadCount: { type: Number, default: 0 },
  index: { type: Number, default: 0 },
  typeTemplateParms: { type: Object },
  noneTypeTemplateParms: { type: Object },
  templateTemplateParms: { type: Object },
  templateParamsData: { type: Object },
  functionAttrs: Array,
  userDefinedFields: [userDefinedFieldSchema],
  sourceParsedFile: { type: Array, index: true },
  insideProject: Boolean,
  manualSpecialized: Boolean,
  collectCoverage: Boolean,
  cycleComplexity: Number, // 圈复杂度
  essentialComplexity: Number, // 基本复杂度
  fanIn: Number, // 扇入
  fanOut: Number, // 扇出
  numberOfLines: { type: Number, default: 0 }, // 文件总行数
  sourceLines: { type: Number, default: 0 }, // 有效代码行数
  commentLines: { type: Number, default: 0 }, // 注释行数
  emptyLines: { type: Number, default: 0 }, // 空白行数
  compatibleKey: String,
  goto: Number, // goto语句数量
  localVariableSizeMax: Number, // 局部变量占用最大值
  loopDepthMax: Number, // 最大循环嵌套深度
  loopCount: Number, // 循环个数
  returnCount: Number, // return语句个数
  exitCount: Number, // 程序出口数
};
